package com.tdam2011.grupo02.ui;

import java.util.LinkedList;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.data.LogsController;
import com.tdam2011.grupo02.domain.MensajeWeb;
import com.tdam2011.grupo02.widget.popup.ActionItem;
import com.tdam2011.grupo02.widget.popup.QuickAction;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class WebMessageLogListFragment extends ListFragment implements
		OnClickDialogFragmentInterface {

	private Context context;
	private MsgwCustomAdapter msgwAdapter;
	private ListView msgwListView;
	private QuickAction msgwPopup;
	private int adapterPosition;
	private MensajeWeb msgw;
	private LogsController controller;
	private WebMessagesAsyncTask webMessagesAsyncTask;

	private final static int POPUP_ACTION_ITEM_DELETE = 12;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListShown(true);

		msgwListView = getListView();

		msgwAdapter = new MsgwCustomAdapter(context);
		msgwListView.setAdapter(msgwAdapter);
		msgwListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				MensajeWeb aux = (MensajeWeb) msgwAdapter.getItem(position);
				Intent r = new Intent(context,
						MsgwConversationListActivity.class);
				r.putExtra("name", aux.getContacto());
				// r.putExtra("imgUri", aux.getImageUri());
				startActivity(r);
			}
		});

		msgwListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				adapterPosition = position;

				int firstPosition = getListView().getFirstVisiblePosition()
						- getListView().getHeaderViewsCount();
				int wantedChild = adapterPosition - firstPosition;

				View v = (View) msgwListView.getChildAt(wantedChild);
				msgwPopup.show(v);

				return false;
			}
		});

		controller = new LogsController(context);
		createMsgwPopup();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		inflater.inflate(R.layout.log_web_messages, container);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onResume() {
		initData();

		super.onResume();
	}

	private void initData() {
		webMessagesAsyncTask = new WebMessagesAsyncTask();
		webMessagesAsyncTask.execute();
	}

	private LinkedList<MensajeWeb> getMensajesWeb() {
		ContactDataInterface data = (ContactDataInterface) context;
		String nombreContacto = data.onDataSelected()[1];

		return controller.getWebMessagesByContact(nombreContacto);
	}

	private class WebMessagesAsyncTask extends
			AsyncTask<String, String, LinkedList<MensajeWeb>> {

		@Override
		protected LinkedList<MensajeWeb> doInBackground(String... arg0) {
			return getMensajesWeb();
		}

		@Override
		protected void onPostExecute(LinkedList<MensajeWeb> result) {
			msgwAdapter.add(result);
			msgwAdapter.notifyDataSetChanged();

			super.onPostExecute(result);
		}
	}

	private void createMsgwPopup() {
		ActionItem eliminarItem = new ActionItem(POPUP_ACTION_ITEM_DELETE,
				getString(R.string.actionDelete), getResources().getDrawable(
						android.R.drawable.ic_menu_delete));

		msgwPopup = new QuickAction(context);
		msgwPopup.addActionItem(eliminarItem);
		msgwPopup
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					@Override
					public void onItemClick(QuickAction quickAction, int pos,
							int actionId) {
						msgw = (MensajeWeb) msgwAdapter
								.getItem(adapterPosition);

						switch (actionId) {
						case POPUP_ACTION_ITEM_DELETE:
							showDialog();
							break;
						}

					}
				});

		msgwPopup.setOnDismissListener(new QuickAction.OnDismissListener() {
			@Override
			public void onDismiss() {
			}
		});
	}

	// Dialog Fragment

	private void showDialog() {
		DialogFragment newFragment = MyAlertDialogFragment.newInstance(this,
				getString(R.string.attentionAlertDialogTitle),
				getString(R.string.deleteItemAlertDialogMessage));
		newFragment.show(getFragmentManager(), "dialog");
	}

	@Override
	public void doPositiveClick() {
		int num = controller.deleteWebMessagesConversation(msgw);
		if (num != 0)
			initData();
	}

	@Override
	public void doNegativeClick() {
	}
}
